/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-01-04
*/
package com.rzico.article.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.article.entity.Recommend;
import com.rzico.article.model.RecommendVo;
import com.rzico.article.service.RecommendService;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Product;
import com.rzico.basics.model.ProductVo;
import com.rzico.basics.model.PromotionVo;
import com.rzico.basics.model.ShopVo;
import com.rzico.basics.service.MemberService;
import com.rzico.basics.service.ProductService;
import com.rzico.basics.service.ShopService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.AreaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * 推荐平台控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2021-01-04
 */
@Api(description = "推荐平台接口")
@RestController
@RequestMapping("/recommend")
public class RecommendController extends BaseController {

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ShopService shopService;

    /**
     * 分页查询推荐平台
     *
     * @return
    */
    @ApiOperation("分页查询推荐平台")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lat", value = "lat", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "lng", value = "lng", dataType = "Double", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "城市ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型{0:商品,1:名店,2:酒店,3:名厨,4.餐车}", dataType = "String", paramType = "query")
    })
    public CommResult<Recommend> list(Date startDate, Date endDate, Integer type,Long areaId,String keyword,Double lat,Double lng, Pageable pageable) {

        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (type!=null){
            params.put("type", type);
        }
        if (keyword!=null){
            params.put("keyword", keyword);
        }

        if (areaId!=null){
            AreaUtils.encodeMap(areaId,params);
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<RecommendVo> list = recommendService.selectLbsList(params,lat,lng);
        for (RecommendVo vo:list) {
            if (vo.getType().equals(0)) {
                Map<String,Object> voParams = new HashMap<>();
                voParams.put("id",vo.getTypeId());

                List<ProductVo> products = productService.selectProductVoList(voParams);
                if (products.size()>0) {
                    ProductVo productVo = products.get(0);
                    vo.setProductVo(productVo);

                    Member member = memberService.getCurrent();

                    if (member!=null && member.getVvip().compareTo(1)>0) {
                        productVo.setVipPrice(productVo.getMemberPrice(member));
                    } else {
                        productVo.setVipPrice(productVo.getVip1price());
                        if (productVo.getVipPrice().compareTo(BigDecimal.ZERO)==0) {
                            productVo.setVipPrice(productVo.getPrice());
                        }
                    }

                    if (member!=null && member.getAmount().compareTo(BigDecimal.ZERO)>0) {
                        List<PromotionVo> pvo = new ArrayList<>();
                        for (PromotionVo pvvo:productVo.getPromotions()) {
                            if (pvvo.getType().equals(10)) {
                                continue;
                            }
                            if (pvvo.getFirsted()) {
                                continue;
                            }
                            if (productVo.getFreightId()==null && vo.getType().equals(6)) {
                                continue;
                            }
                            pvo.add(pvvo);
                        }
                        productVo.setPromotions(pvo);
                    }
                }

            } else
            {
                Map<String, Object> voParams = new HashMap<String, Object>();

                voParams.put("id",vo.getTypeId());

                List<ShopVo> shopVo = shopService.selectLbsList(voParams,lat,lng);

                if (shopVo.size()>0) {
                    vo.setShopVo(shopVo.get(0));
                }

            }
        }
        PageResult<RecommendVo> pageResult = new PageResult<RecommendVo>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

}
